package fr.upmc.aladyn.test;

import fr.upmc.aladyn.transactionables.Transactionable;





/**
 * 
 * Test class for the backup and restore method. Contains the radius,
 * the circle center coordinates , and a color.
 * @author Ritchie Nithoo 
 * @author Jules Fabry
 * @version 1.0
 */
public class Cercle {
	private int rayon;
	private MyPointColore point;
	
	
	/**
	 * 
	 * @param r The radius of the circle
	 * @param x The x coordinate of the center
	 * @param y The y coordinate of the center
	 * @param c The circle color
	 */
	public Cercle(int r, int x, int y, Couleur c){
		rayon=r;
		point=new MyPointColore(c, x, y);
	}
	
	/**
	 * Returns the color of the circle
	 * @return The color of the circle's center
	 */
	public MyPointColore getPointColore(){
		return point;
	}
	
	/**
	 * Sets the "point" attributes
	 * @param p The point on which we want to change the color
	 */
	public void setPointColore(MyPointColore p){
		point=p;
	}
	
	
	/**
	 * Throws an exception divided by zero in order to test the save and backup, and modifies the "point"attribute.
	 */
	@Transactionable
	public void modifier1(){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point.setX(3);
			point.setCouleur(new Couleur("red"));
			point.setX(point.getX()/0);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	/**
	 * Modifies the color of the point
	 * @param p The point that we want to modify
	 * 
	 */
	@Transactionable
	public void modifier2(MyPoint p){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point.setCouleur(new Couleur("blue"));
			p.add(2);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	
	/**
	 * 
	 * Modifies the color of p and the X coordinates. Divides it by zero.
	 * @param p  The point that we want to modify
	 * 
	 */
	@Transactionable
	public void modifier3(MyPoint p){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point.setCouleur(new Couleur("blue"));
			p.add(2);
			point.setX(point.getX()/0);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	/**
	 * Calls another @transactionable method and modifies the color of point
	 * @param p The point we want to modify
	 * @param point2 The point that we want to affect
	 */
	@Transactionable
	public void modifier4(Cercle p, MyPoint point2){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point.setCouleur(new Couleur("blue"));
			p.modifier5(point2);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	/**
	 * The setX throws an divided by zero exception
	 * @param point2 The point we want to modify
	 */
	@Transactionable
	public void modifier5(MyPoint point2){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point2.setX(2);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	/**
	 * 
	 * @param p
	 * @param point2
	 */
	@Transactionable
	public void modifier6(Cercle p, MyPoint point2){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point.setCouleur(new Couleur("blue"));
			p.modifier7(point2);
			point.setX(point.getX()/0);
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
	
	/**
	 * This method goes through a number of get to finally do a set and check whether the
	 * point has been correctly restored or not.
	 * @param point2 The point we want to modify
	 */
	@Transactionable
	public void modifier7(MyPoint point2){
		try{
			fr.upmc.aladyn.transactionables.BackUpManager.begin();
			point2.setX(2);
			getPointColore().getCouleur().setCouleur("blue");
			fr.upmc.aladyn.transactionables.BackUpManager.end();
		}
		catch(Exception e){
			fr.upmc.aladyn.transactionables.BackUpManager.restore();
			fr.upmc.aladyn.transactionables.BackUpManager.end();
			throw e;
		}
	}
}
